import Vue from 'vue'
import VueRouter from 'vue-router'

import Home from '../views/Home/index.vue'
import Login from '../views/Login/index.vue'


Vue.use(VueRouter)

const router = new VueRouter({
    routes:[
        {
            name:'home',
            path:'/home',
            component:Home,
            beforeEnter:(to,from,next)=>{
                console.log('beforeEnter',to,from);
                next()
            }
        },
        {
            name:'login',
            path:'/login',
            component:Login
        },
        {
            path:'/',
            redirect:'home'
        }
    ]
})

const originPush = VueRouter.prototype.push
VueRouter.prototype.push = function (location, onResolved, onRejected) {
  console.log('push', onResolved, onRejected)
//   debugger
  // 如果传入了回调, 用的旧语法, 不会有错误的, 调用原函数进行处理
  if (onResolved || onRejected) {
    originPush.call(this, location, onResolved, onRejected)
  } else { // 没有传回调, 用的新语法, 可能会抛出失败的promise错误
    return originPush.call(this, location).catch(() => {
      console.log('error')
    })
    // originPush.call(this, () => {})
  }
}

// router.beforeEach((to,from,enxt)=>{
//     console.log('beforeEach',to,from);
//     // if(Date.now()%2===1){
//     //     enxt('/login')
//     // }else{
//     //     enxt()
//     // }
//     enxt()
// })

// router.beforeResolve((to,from,next)=>{
//     console.log('beforeResolve',to,from);
//     next()
// })

// router.afterEach((to,from)=>{
//     console.log('afterEach',to,from);
    
// })
export default router